<?php

namespace App\Models;

use App\Traits\Model as ModelTrait;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserSecurity extends Model
{
    protected $table = 'user_security';
    use HasFactory;
    use ModelTrait;
    use SpatialTrait;

    protected $spatialFields = [
        'position'
    ];

    public function save(array $options = [])
    {
        unset($this->attributes['latitude']);
        unset($this->attributes['longitude']);
        return parent::save($options); // TODO: Change the autogenerated stub
    }

    public function getLatitudeAttribute(){
        return $this->attributes['position']->getLat();
    }

    public function getLongitudeAttribute(){
        return $this->attributes['position']->getLng();
    }


    protected function setPositionAttribute($value){
        list($lat, $lng) = explode(',', $value);
        $this->attributes['position'] = new Point($lat, $lng);
    }
}
